class Solution
{
public:
    TreeNode* sortedArrayToBST(vector<int>& nums)
    {
        return build(nums, 0, nums.size() - 1);
    }

    TreeNode* build(vector<int>& nums, int l, int r)
    {
        if (l > r)  return nullptr;

        int mid = l + r >> 1;

        TreeNode* ans = new TreeNode(nums[mid]);

        ans->left = build(nums, l, mid - 1);
        ans->right = build(nums, mid + 1, r);

        return ans;
    }
};